//package com.corpgovernment.common.aspect;
//
//import com.alibaba.fastjson.JSONObject;
//import com.corpgovernment.common.apollo.CommonApollo;
//import com.corpgovernment.common.common.CorpBusinessException;
//import com.corpgovernment.common.constant.CommonConst;
//import com.corpgovernment.common.enums.ExceptionCodeEnum;
//import com.corpgovernment.common.holder.TenantContextHolder;
//import com.corpgovernment.common.utils.TenantUtils;
//import com.ctrip.corp.obt.generic.exception.CommonException;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.Objects;
//
///**
// * 商旅政务项目数据源切面
// *
// * @author jhxue
// */
//@Aspect
//@Component
//@Slf4j
//@Order(-1)
//public class DataSourceAspect {
//
//	@Autowired
//	@Qualifier(value = "commonApollo")
//	private CommonApollo commonApollo;
//
//	@Pointcut(value = "execution(* com.corpgovernment..*.controller..*.*(..)) && execution(public * *(..)) && !@annotation(com.corpgovernment.common.annotation.ExternalCallGetTenant)")
//	private void cutController() {
//
//	}
//
//	/**
//	 * 根据每个服务的包名，定义controller切面日志 切controller和public
//	 *
//	 * @param pjp
//	 * @return
//	 */
//	@Around("cutController()")
//	public Object around(ProceedingJoinPoint pjp) {
//		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//		if (attributes == null) {
//			throw new CorpBusinessException(ExceptionCodeEnum.Alert, "request attributes is null");
//		}
//		HttpServletRequest request = attributes.getRequest();
//		// 租户标识
//		String tenantId = this.getTenantId(request);
//		String methodName = pjp.getSignature().getName();
//		Object result = null;
//		try {
//			TenantContextHolder.setTenantId(tenantId);
//			result = pjp.proceed();
//			log.info("请求:{},返回结果为：{}", methodName, JSONObject.toJSONString(result));
//		} catch (CommonException ex) {
//			throw ex;
//		} catch (Throwable e) {
//			log.error("请求" + methodName + ",异常为", e);
//			throw new CorpBusinessException(ExceptionCodeEnum.Alert, e.getMessage());
//		} finally {
//			TenantContextHolder.clearTenantId();
//		}
//		return result;
//	}
//
//	public String getTenantId(HttpServletRequest request) {
//		String tenantId = request.getHeader(CommonConst.COMMON_REQUEST_HEADER);
//		TenantContextHolder.setTenantId(tenantId);
//		String systemModel = commonApollo.getSystemModel();
//		boolean isTenant = TenantUtils.isTenant();
//		log.info("当前系统模式：{}，请求租户id：{}，是否为租户请求：{}", systemModel, tenantId, isTenant);
//		// 租户标识
//		if (Objects.equals(systemModel, CommonConst.DEFAULT_SYSTEM_MODEL)) {
////			if (isTenant) {
////				log.info("单用户系统，无法接收租户id：[{}]的请求", tenantId);
//				return null;
////			}
//		} else {
//			if (!isTenant) {
//				log.info("多租户系统，无法接收请求头没有租户id的请求");
//				throw new CorpBusinessException(ExceptionCodeEnum.Alert, CommonConst.MULTI_SYSTEM_ERROR);
//			}
//		}
//		return tenantId;
//	}
//
//}
